package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;


public class TabHostNavView extends LinearLayout {

    private Context mContext;
    public View mLineV;
    public LinearLayout mTabNavLly;
    // 导航内容
    public List<String> mNavNamesLs = new ArrayList<>();
    // 引导线高度
    private int mLineHeight = 5;
    // 通用间隔
    private int mDistance = 5;

    private int[]  imgs ;
    private int[] colors;
    public TabHostNavView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public TabHostNavView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public TabHostNavView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    public void setmNavNamesLs(List<String> mNavNamesLs) {
        this.mNavNamesLs = mNavNamesLs;
        initView();
    }
    public void setmNavNamesLs(List<String> mNavNamesLs,int[] imgs) {
        this.imgs=imgs;
        this.mNavNamesLs = mNavNamesLs;
        initView();
    }public void setmNavNamesLs(List<String> mNavNamesLs,int[] imgs,int[] colors) {
        this.colors=colors;
        this.imgs=imgs;
        this.mNavNamesLs = mNavNamesLs;
        initView();
    }
    public void initView() {

        // 设置默认导航栏内容，以便xml引用直接查看效果
        if (mNavNamesLs.isEmpty()) {
            mNavNamesLs.add("TAB_ONE");
            mNavNamesLs.add("TAB_TWO");
        }

        this.removeAllViews();
        LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(params);
        this.setOrientation(VERTICAL);
        this.setBackgroundResource(R.color.gray_bg);

        mTabNavLly = new LinearLayout(mContext);
        mTabNavLly.setLayoutParams(params);
        mTabNavLly.setOrientation(HORIZONTAL);
        for (int i = 0, k = mNavNamesLs.size(); i < k; i++) {
            params = new LayoutParams(0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.weight=1;
            TextView mNavTv = new TextView(mContext);
            mNavTv.setLayoutParams(params);
            mNavTv.setText(mNavNamesLs.get(i));
            if(imgs!=null) {
                mNavTv.setCompoundDrawablesWithIntrinsicBounds(null, getContext().getResources().getDrawable(imgs[i]), null, null);
            }

            if(colors!=null){
                mNavTv.setTextColor(getResources().getColor(i == 0 ? colors[0] : R.color.font_black_think));
            }else {
                mNavTv.setTextColor(getResources().getColor(i == 0 ? R.color.theme_color : R.color.font_black_think));
            }

            mNavTv.setTextSize(14.0f);
            mNavTv.setPadding(mDistance, mDistance, mDistance, mDistance);
            mNavTv.setGravity(Gravity.CENTER);
            mNavTv.setTag(i);
            mNavTv.setOnClickListener(mOnclick);
            mTabNavLly.addView(mNavTv);
        }
        this.addView(mTabNavLly);

        int lineWidth = getItemWidth() - 2 * mDistance;
        params = new LayoutParams(lineWidth, mLineHeight);
        params.leftMargin = mDistance;
        if (mLineV == null)
            mLineV = new View(mContext);
        mLineV.setLayoutParams(params);

        if(colors!=null){
            mLineV.setBackgroundColor(getResources().getColor(colors[0]));
        }else {
            mLineV.setBackgroundColor(getResources().getColor(R.color.theme_color));
        }
        this.addView(mLineV);
    }

    public void setIndicatorLinePosition(int position){
        LayoutParams params = (LayoutParams) mLineV.getLayoutParams();
        params.leftMargin = position+mDistance;
        mLineV.setLayoutParams(params);
    }

    public int getItemWidth(){
        if(mNavNamesLs==null||mNavNamesLs.size()==0)return 0;
        return DensityUtil.getScreenWidth()/mNavNamesLs.size();

    }

    /**
     * @param position 刷新选中项
     */
    public void refreshTab(int position) {
        if (mTabNavLly == null || position < 0) return;
        for (int i = 0, k = mTabNavLly.getChildCount(); i < k; i++) {
            if (mTabNavLly.getChildAt(i) instanceof TextView) {
                if(colors!=null) {
                    ((TextView) mTabNavLly.getChildAt(i))
                            .setTextColor(getResources().getColor(i == position ? colors[position] : R.color.font_gray_light));
                    mLineV.setBackgroundResource(colors[position]);
                }
                else {
                    ((TextView) mTabNavLly.getChildAt(i))
                            .setTextColor(getResources().getColor(i == position ? R.color.theme_color : R.color.font_gray_light));
                }
            }
        }
    }
    OnClickListener mOnclick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnTabSelectListener != null)
                mOnTabSelectListener.onTabSelect((int) v.getTag());
        }
    };

    private OnTabSelectListener mOnTabSelectListener;

    public void setOnTabSelectListener(OnTabSelectListener listener) {
        this.mOnTabSelectListener = listener;
    }

    public interface OnTabSelectListener {
        void onTabSelect(int position);
    }
}
